package com.design.movieticket.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.design.movieticket.dto.DashBoardDto;
import com.design.movieticket.entity.Cinema;
import com.design.movieticket.mapper.CinemaMapper;
import com.design.movieticket.mapper.DashBoardMapper;
import com.design.movieticket.service.DashBoardService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class DashBoardServiceImpl extends ServiceImpl<DashBoardMapper, DashBoardDto> implements DashBoardService {
    @Resource
    private DashBoardMapper dashBoardMapper;

    @Resource
    private CinemaMapper cinemaMapper;

    @Override
    public List getLine(String username,String userRole) {
        List<Integer> list = new ArrayList();
        list.add(0,0);
        list.add(1,0);
        list.add(2,0);
        list.add(3,0);
        list.add(4,0);
        list.add(5,0);
        list.add(6,0);
//        //添加日期数组
//        List<String> dateList = new ArrayList<String>();
//        Collections.addAll(dateList,"4月30日","5月1日","5月2日","5月3日","5月4日","5月5日","5月6日");
        //获取对应日期数据
        String[] dateStartList = new String[]{"2023-04-30 00:00:00","2023-05-01 00:00:00","2023-05-02 00:00:00","2023-05-03 00:00:00","2023-05-04 00:00:00","2023-05-05 00:00:00","2023-05-06 00:00:00"};
        String[] dateEndList = new String[]{"2023-04-30 23:59:59","2023-05-01 23:59:59","2023-05-02 23:59:59","2023-05-03 23:59:59","2023-05-04 23:59:59","2023-05-05 23:59:59","2023-05-06 23:59:59"};
        if (userRole.equals("principal")){
            for (int i = 0 ;i < 7; i++){
                String dateStart = dateStartList[i];
                String dateEnd = dateEndList[i];
                String tar = dashBoardMapper.getLine(username,dateStart,dateEnd);
                if (tar== null){
                    tar = "0";
                }
                list.add(i,Integer.parseInt(tar));
            }
        }
        else if (userRole.equals("admin")){
            List<String> listPrincipal = new ArrayList();
            List<String> cinemaName = new ArrayList();
            List<Cinema> cinemaPrincipal = cinemaMapper.selectList(new LambdaQueryWrapper<Cinema>().eq(Cinema::getAdmin,username));
            for (int i = 0; i < cinemaPrincipal.size(); i++){
                listPrincipal.add(i, cinemaPrincipal.get(i).getPrincipal());
                cinemaName.add(i,cinemaPrincipal.get(i).getCinemaName());
            }
            for (int i = 0; i < cinemaPrincipal.size();i++){
                for (int j = 0 ;j < 7; j++){
                    String dateStart = dateStartList[j];
                    String dateEnd = dateEndList[j];
                    String temp = dashBoardMapper.getLine(listPrincipal.get(i),dateStart,dateEnd);
                    System.out.println(temp);
                    if (temp == null){
                        temp = "0";
                    }
                    list.set(j,list.get(j) + Integer.parseInt(temp));
                }
            }

        }
        else{
            for (int i = 0 ;i < 7; i++){
                String dateStart = dateStartList[i];
                String dateEnd = dateEndList[i];
                String tar = dashBoardMapper.getLineBySuperAdmin(username,dateStart,dateEnd);
                list.add(i,Integer.parseInt(tar));
            }
        }
        return list;
    }

    @Override
    public List<String> getCircle() {
        return dashBoardMapper.getCircle();
    }

    @Override
    public List<String> getData() {
        return dashBoardMapper.getData();
    }
}
